package ch.ffhs.bscinf2009.organizeanything.gui;

import java.util.List;
import ch.ffhs.bscinf2009.organizeanything.Environment;
import ch.ffhs.bscinf2009.organizeanything.R;
import ch.ffhs.bscinf2009.organizeanything.data.Organizable;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * @author lir
 * This is the home screen listing all available object definitions
 */
public class OrganizableTypeList extends Activity {
	
	@Override
	public void onBackPressed() {
		// do not go back to avoid refresh issues. user uses the home button to leave the application
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.organizable_type_list);

		ListView listView = (ListView) findViewById(R.id.organizable_type_list);
		
		final List<Organizable> orgs = Environment.getInstance().getDB(this).readDefinitions();
		String[] values = new String[orgs.size()];
		int i = 0;
		for (Organizable organizable : orgs) {
			values[i] = organizable.getOrganizableName();
			i++;
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);

		// Assign adapter to ListView
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
				int position, long id) {
				final Intent i = new Intent(OrganizableTypeList.this, OrganizableInstanceList.class);
				i.putExtra("definition", orgs.get(position));
				startActivity(i);
			}
		});
	}
	
	/**
	 * shows the create new object type screen
	 * @param view the view the method was called from
	 */
	public void createObjectType(View view){
		final Intent i = new Intent(OrganizableTypeList.this, CreateNewObjectType.class);
		startActivity(i);
	}
}
